package br.com.medvagas.comum.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class UtilCriptografia {

	private static MessageDigest md;
	
	private static final char hexDigit[] = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	static {
		try {
			md = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
	}

	public static String criptografarSha(String texto) {
		return bytesToHex(md.digest(texto.getBytes()));
	}

	public static String bytesToHex(byte[] b) {
		StringBuilder buf = new StringBuilder();
		for (int j = 0; j < b.length; j++) {
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(criptografarSha(""));
		System.out.println(criptografarSha("123"));
		System.out.println(criptografarSha("asdf123"));
	}
	
}